package com.test.exam.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.test.exam.domain.Question;
import com.test.exam.service.QuestionService;
import com.test.exam.service.SubjectService;


@Controller
public class QuestionController {
	@Autowired
	private QuestionService questionService;
	@Autowired
	private SubjectService subjectService;
	
	//페이지 문제 목록
	@RequestMapping(
			value="/admin/question/getQuestionsByCurrentPage",
			method=RequestMethod.GET
			)
	public String getQuestionsByCurrentPage(
			@RequestParam(defaultValue="0") int subjectIdx,
			@RequestParam(defaultValue="1") int currentPage,
			Model model){
		
	//	System.out.println("subjectIdx:"+subjectIdx);
	//	System.out.println("currentpage:"+currentPage);
		
		model.addAttribute(
				"questionList", 
				questionService.getQuestionsByCurrentPage(currentPage, subjectIdx));
		
		model.addAttribute("subjectList", subjectService.getAllSubjects());
		//현재 검색조건을 view로 넘겨주는 변수
		model.addAttribute("subjectIdx",subjectIdx);
		
		model.addAttribute("currentPage", currentPage);
		model.addAttribute("endPage", questionService.getEndPage(subjectIdx));
		
		return "/admin/question/questionList";
	}
	
	@RequestMapping(
			value="/admin/question/addQuestion",
			method=RequestMethod.POST)
	public String addQuestion(Question question,
						@RequestParam(value="exampleContent") String[] exampleContent){
		
		questionService.addQuestion(question, exampleContent);
		return "redirect:/admin/question/getQuestionsByCurrentPage";
	}
	
	
	@RequestMapping(
			value="/admin/question/addQuestionForm")
	public String addQuestionForm(Model model){
		model.addAttribute("subjectList", subjectService.getAllSubjects());
		return "/admin/question/addQuestionForm";
	}
	
	
}











